package com.noah.bee.admin.service;

import com.noah.bee.admin.domain.User;
import com.noah.bee.admin.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 吴镇
 * @description:
 * @createdate 2016/12/29
 */
@Service
public class UserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    public User getUser(String userName, String password) throws NoSuchAlgorithmException {
        User user = userMapper.getUserByUserNameAndPassword(userName,md5(password));
        return user;
    }

    private String md5(String str) throws NoSuchAlgorithmException {
        BASE64Encoder base64en = new BASE64Encoder();
        String md5Str = null;
        try {
            md5Str = base64en.encode(MessageDigest.getInstance("MD5").digest(str.getBytes()));
        } catch (NoSuchAlgorithmException e) {
            LOGGER.error("加解密错误：{}",e);
            throw e;
        }
        return md5Str;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String userName = "111111";
        UserService userService = new UserService();
        System.out.println(userService.md5(userName));
    }
}
